﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Data;
namespace Door_Common
{
    public class UploadCommander
    {               
        public static string UserImageMap = "UserUpFile";
        public static string dirpath = System.Web.HttpContext.Current.Request.MapPath("../" + UserImageMap);
        public static List<string> UploadFile()
        {
            List<string> FileNameList = new List<string>();
            string NewName = String.Empty;
            System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;             
                //以下为创建上传目录
            string dirpath = HttpContext.Current.Server.MapPath("../" + UserImageMap);

                if (Directory.Exists(dirpath) == false)
                {
                    Directory.CreateDirectory(dirpath);
                }
                Random ro = new Random();
                int name = 1;
                for (int i = 0; i < files.Count; i++)
                {
                    System.Web.HttpPostedFile myFile = files[i];
                    string FileName = "";
                    string FileExtention = "";
                    FileName = System.IO.Path.GetFileName(myFile.FileName);
                    string stro = ro.Next(1, 100000000).ToString() + name.ToString();//产生一个随机数用于新命名的图片  
                    NewName = System.DateTime.Now.ToString("yyyyMMddHHmmss") + stro;

                    if (FileName.Length > 0)//有文件才执行上传操作再保存到数据库
                    {
                        FileExtention = System.IO.Path.GetExtension(myFile.FileName);

                        string ppath = dirpath + @"\" + NewName + FileExtention;
                        NewName += FileExtention;
                        myFile.SaveAs(ppath);
                        FileNameList.Add(ppath);
                    }
                    name = name + 1;//用来重命名规则的变量
            } 
                return FileNameList;            
        }
        public static string UploadSingleFile()
        {
            List<string> FileNameList = new List<string>();
            string NewName = String.Empty;
            System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            //以下为创建上传目录


            if (Directory.Exists(UploadCommander.dirpath) == false)
            {
                Directory.CreateDirectory(UploadCommander.dirpath);
            }
            Random ro = new Random();
            int name = 1;
            for (int i = 0; i < files.Count; i++)
            {
                System.Web.HttpPostedFile myFile = files[i];
                string FileName = "";
                string FileExtention = "";
                FileName = System.IO.Path.GetFileName(myFile.FileName);
                string stro = ro.Next(1, 100000000).ToString() + name.ToString();//产生一个随机数用于新命名的图片  
                NewName = System.DateTime.Now.ToString("yyyyMMddHHmmss") + stro;

                if (FileName.Length > 0)//有文件才执行上传操作再保存到数据库
                {
                    FileExtention = System.IO.Path.GetExtension(myFile.FileName);

                    string ppath = dirpath + @"\" + NewName + FileExtention;
                    NewName += FileExtention;
                    myFile.SaveAs(ppath);
                    FileNameList.Add(NewName);
                }
                name = name + 1;//用来重命名规则的变量
            }
            if (FileNameList.Count > 0)
                return FileNameList[0];
            else return  "";
        }

        /// <summary>
        /// 验证上传的图片格式
        /// </summary>
        /// <returns></returns>
        public static bool ValidateExtention(ref string strError)
        {
            System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            System.Text.StringBuilder strmsg = new System.Text.StringBuilder("");
            int ifile;
            for (ifile = 0; ifile < files.Count; ifile++)
            {
                if (files[ifile].FileName.Length > 0)
                {
                    System.Web.HttpPostedFile postedfile = files[ifile];                   
                    string fex = Path.GetExtension(postedfile.FileName).ToLower();
                    if (fex != ".jpg"  && fex != ".gif" &&  fex != ".bmp" && fex!=".png"&& fex !="jpeg")
                    {
                        strmsg.Append(Path.GetFileName(postedfile.FileName) + "---图片格式不对，只能是.jpg/.gif/.bmp/.png其中一种！<br>");
                        strError = strmsg.ToString();
                        return false;
                    }
                }
            }
            return true;
        }
        /// <summary>
        /// 验证图片大小
        /// </summary>
        /// <returns></returns>
        public static bool ValidateSize(ref string strError)
        {
            List<string> FileNameList = new List<string>();
            string NewName = String.Empty;
            System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            System.Text.StringBuilder strmsg = new System.Text.StringBuilder("");
            int ifile;
            for (ifile = 0; ifile < files.Count; ifile++)
            {
                if (files[ifile].FileName.Length > 0)
                {
                    System.Web.HttpPostedFile postedfile = files[ifile];
                    if (postedfile.ContentLength / 1024 > 5120)//单个文件不能大于5M
                    {
                        strmsg.Append(Path.GetFileName(postedfile.FileName) + "---不能大于5M<br>");
                        strError = strmsg.ToString(); 
                        return false ;
                    } 
                }
            }
            return true;
        }
    }
}
